//
//  ContactDateHelper.m
//  Pods
//
//  Created by 展祥叶 on 2017/7/14.
//
//

#import "ContactDateHelper.h"

@interface ContactDateHelper ()
@property (nonatomic, strong) NSArray *dayNames;
@property (nonatomic, strong) NSArray *daySammNames;
@end

@implementation ContactDateHelper

+ (instancetype)sharedInstance {
    static ContactDateHelper *helper = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper = [[ContactDateHelper alloc] init];
    });
    return helper;
}

- (NSString *)stringWithDate:(NSDate *)date {
    float different = [date timeIntervalSinceNow];
    
    if (different > 0) {
        return @"Time Error!";
    } else {
        
        // 当前时间
        NSDate *currentDate = [NSDate date];
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *currentComps = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:currentDate];
        NSInteger currentYear = [currentComps year];
        NSInteger currentMonth = [currentComps month];
        NSInteger currentDay = [currentComps day];
        
        // 传入时间
        NSDateComponents *comps = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday) fromDate:date];
        NSInteger year = [comps year];
        NSInteger month = [comps month];
        NSInteger day = [comps day];
        NSInteger weekday = [comps weekday];
        
        // 传入时间的时分
        comps =[calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute)
                           fromDate:date];
        NSInteger hour = [comps hour];
        NSInteger minute = [comps minute];
        
        NSString *todayStr = [NSString stringWithFormat:@"%ld-%ld-%ld 23:59:59", currentYear, currentMonth, currentDay];
        different = - [date timeIntervalSinceDate:[self dateFromString:todayStr]];
        float dayDifferent = floor(different / 86400);
        
        if (dayDifferent < 1) {
            return [NSString stringWithFormat:@"%.2ld:%.2ld", hour, minute];
        } else if (dayDifferent < 2) {
            return [NSString stringWithFormat:@"昨天 %.2ld:%.2ld", hour, minute];
        } else if (dayDifferent < 7) {
            NSString *weekdayStr = [NSString string];
            switch (weekday) {
                case 1:
                    weekdayStr = @"星期日";
                    break;
                case 2:
                    weekdayStr = @"星期一";
                    break;
                case 3:
                    weekdayStr = @"星期二";
                    break;
                case 4:
                    weekdayStr = @"星期三";
                    break;
                case 5:
                    weekdayStr = @"星期四";
                    break;
                case 6:
                    weekdayStr = @"星期五";
                    break;
                case 7:
                    weekdayStr = @"星期六";
                    break;
                default:
                    break;
            }
            return [NSString stringWithFormat:@"%@ %.2ld:%.2ld", weekdayStr, hour, minute];
        } else if (year == currentYear) {
            return [NSString stringWithFormat:@"%ld-%ld %.2ld:%.2ld", month, day, hour, minute];
        } else {
            return [NSString stringWithFormat:@"%ld-%ld-%ld %.2ld:%.2ld", year, month, day, hour, minute];
        }
    }
}

- (NSDate *)dateFromString:(NSString *)dateString {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    NSDate *destDate= [dateFormatter dateFromString:dateString];
    return destDate;
}

-(NSDateComponents *)pleaseInsertStarTimeo:(NSString *)time1 andInsertEndTime:(NSString *)time2{
    // 1.将时间转换为date
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date1 = [formatter dateFromString:time1];
    NSDate *date2 = [formatter dateFromString:time2];
    // 2.创建日历
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit type = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    // 3.利用日历对象比较两个时间的差值
    NSDateComponents *cmps = [calendar components:type fromDate:date1 toDate:date2 options:0];
    // 4.输出结果
    NSLog(@"两个时间相差%ld年%ld月%ld日%ld小时%ld分钟%ld秒", cmps.year, cmps.month, cmps.day, cmps.hour, cmps.minute, cmps.second);
    return cmps;
}

/*
 秒转化分钟
 */
-(NSString *)getMMSSFromSS:(NSString *)totalTime{
    NSInteger seconds = [totalTime integerValue];
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
    
    NSString *format_time;
    if ([str_hour isEqualToString:@"00"] && [str_minute isEqualToString:@"00"]) {
        format_time = [NSString stringWithFormat:@"%@秒",str_second];
    }else if ([str_hour isEqualToString:@"00"]) {
        format_time = [NSString stringWithFormat:@"%@分%@秒",str_minute,str_second];
    }else{
        format_time = [NSString stringWithFormat:@"%@小时%@分%@秒",str_hour,str_minute,str_second];
    }
    return format_time;
}

@end
